package com.cn.wanxi.util;

import sun.misc.BASE64Encoder;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Tool {
    public static int stringToInt(String value) {
        try {
            return Integer.parseInt(value);
        } catch (Exception e) {

        }
        return -1;
    }

    public static int stringToPageNum(String value) {
        try {
            return Integer.parseInt(value);
        } catch (Exception e) {

        }
        return 1;
    }

    public static int stringToPageSize(String value) {
        try {
            return Integer.parseInt(value);
        } catch (Exception e) {

        }
        return 10;
    }

    public static String nullToString(String value) {

        return value == null ? "" : value;

    }

    /**
     * 利用MD5进行加密
     */
    public static String EncoderByMd5(String str) {
        //确定计算方法
        MessageDigest md5 = null;
        String newstr = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
            BASE64Encoder base64en = new BASE64Encoder();
            //加密后的字符串
            newstr = base64en.encode(md5.digest(str.getBytes("utf-8")));
        } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return newstr;
    }
}
